//
//  ZhaohuanshiViewController.m
//  A_HandHeldLOL
//
//  Created by Zhao YanBin on 16/1/22.
//  Copyright © 2016年 zhaoyanbin. All rights reserved.
//

#import "ZhaohuanshiViewController.h"
#import "ZhsCollectionViewCell.h"
#import "ZhaohuanshiModel.h"
#import "ZhsJnViewController.h"
@interface ZhaohuanshiViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *collection;

@property(nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation ZhaohuanshiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self creatcollection];
    
    [self data];
}

//collectionView
-(void)creatcollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //cell的内容大小
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 50 )/ 4 , (self.view.frame.size.width - 50 )/ 4 );
    
    //上左下右
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //cell的间距
    layout.minimumLineSpacing = 10;
    
    _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.tag = 3000;
    
    //整页翻动
    _collection.pagingEnabled = YES;
    [self.view addSubview:_collection];
    
    //注册重用池
    [_collection registerClass:[ZhsCollectionViewCell class] forCellWithReuseIdentifier:@"zhaohuanshi"];
    [layout release];
    [_collection release];
}

-(void)data
{
    
    NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Zhaohuanshi" ofType:@"plist"]];
    self.dataArr = [ZhaohuanshiModel modelHanderWithArray:arr];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZhsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"zhaohuanshi" forIndexPath:indexPath];;
    ZhaohuanshiModel *m = _dataArr[indexPath.item];
    [cell.img setImage:[UIImage imageNamed:m.image]];
    cell.name.text = m.name;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZhsJnViewController *zhsjnVC = [[ZhsJnViewController alloc] init];
    zhsjnVC.i = indexPath.item;
    [self.navigationController pushViewController:zhsjnVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
